package org.wlw.arr;

public class TestSort01 {
    public static void main(String[] args) {
        int[] nums = {123,4,1,4,123,3,12,4,12,4,51,12,41,51,23};
        printArr(nums);
        bubbleSort(nums);
        printArr(nums);
    }

    //不稳定O(n的平方)
    public static void selectSort(int[] nums) {
        for(int i=0;i<nums.length;i++) {
            int k = i;
            for(int j=i+1;j<nums.length;j++) {
                if(nums[k]>nums[j]) {
                    k = j;
                }
            }
            if(k!=i) {
                int t = nums[k];
                nums[k] = nums[i];
                nums[i] = t;
            }
        }
    }

    //稳定(O(n的平方))
    public static void bubbleSort(int[] nums) {
        for(int i=nums.length-1;i>0;i--) {
            for(int j=0;j<i;j++) {
                if(nums[j]>nums[j+1]) {
                    int t = nums[j];
                    nums[j] = nums[j+1];
                    nums[j+1] = t;
                }
            }
        }
    }

    public static void printArr(int[] nums) {
        for(int i=0;i<nums.length;i++) {
            System.out.print(nums[i]+",");
        }
        System.out.println();
    }
}
